<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.userName" placeholder="用户名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getList">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="usertAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tableData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" label="序号" width="60">
			</el-table-column>
			<el-table-column prop="userName" label="用户姓名" width="120">
			</el-table-column>
			<el-table-column prop="userType" label="用户类型" width="120" :formatter="formatUserType">
			</el-table-column>
			<el-table-column prop="userPinyin" label="姓名拼音" width="120">
			</el-table-column>
			<el-table-column prop="loginAccount" label="登入账号" min-width="120">
            </el-table-column>
			<!--<el-table-column prop="idcard" label="身份证" min-width="120">-->
            <!--</el-table-column>-->
			<el-table-column prop="badgeNumber" label="警号" min-width="120">
            </el-table-column>
			<el-table-column prop="tel" label="电话号码" min-width="120">
			</el-table-column>
			<el-table-column prop="createDate" label="创建时间" min-width="120" :formatter="formatCreateDate">
			</el-table-column>
			<el-table-column prop="updateDate" label="修改时间" min-width="120" :formatter="formatCreateDate">
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="userEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="userDelete(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
            <el-pagination layout="total, prev, pager, next, jumper" @current-change="handleCurrentPageChange"
                            :current-page="pageNum" :page-size="pageSize" :total="total">
            </el-pagination>
        </el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :modal-append-to-body="false">
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
				<el-form-item label="用户姓名" prop="marketName">
					<el-input v-model="editForm.userName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户类型">
					<el-radio-group v-model="editForm.userType">
						<el-radio class="radio" :label="0">编制员工</el-radio>
						<el-radio class="radio" :label="1">合同制员工</el-radio>
                        <el-radio class="radio" :label="2">第三方员工</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="姓名拼音">
					<el-input v-model="editForm.userPinyin"></el-input>
				</el-form-item>
				<el-form-item label="登入账号">
					<el-input v-model="editForm.loginAccount"></el-input>
				</el-form-item>
                <el-form-item label="身份证">
					<el-input v-model="editForm.idcard"></el-input>
				</el-form-item>
                <el-form-item label="警号">
					<el-input v-model="editForm.badgeNumber"></el-input>
				</el-form-item>
                <el-form-item label="电话号码">
					<el-input v-model="editForm.tel"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :modal-append-to-body="false">
			<el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
				<el-form-item label="用户姓名" prop="userName">
					<el-input v-model="addForm.userName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户类型">
					<el-radio-group v-model="addForm.userType">
						<el-radio class="radio" :label="0">编制员工</el-radio>
						<el-radio class="radio" :label="1">合同制员工</el-radio>
                        <el-radio class="radio" :label="2">第三方员工</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="姓名拼音">
					<el-input v-model="addForm.userPinyin"></el-input>
				</el-form-item>
				<el-form-item label="登入账号">
					<el-input v-model="addForm.loginAccount"></el-input>
				</el-form-item>
                <el-form-item label="身份证">
					<el-input v-model="addForm.idcard"></el-input>
				</el-form-item>
                <el-form-item label="警号">
					<el-input v-model="addForm.badgeNumber"></el-input>
				</el-form-item>
                <el-form-item label="电话号码">
					<el-input v-model="addForm.tel"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	import util from '../api/util'
	//import NProgress from 'nprogress'
	import { getUserList,addUser,editUser,deleteUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					marketNmae: ''
				},
				tableData: [],
				total: 0,
				pageNum: 1,
				pageSize:2,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					userName: [
						{ required: true, message: '请输入用户姓名', trigger: 'blur' }
					],
				},
				//编辑界面数据
				editForm: {
					userName:'',
					userType: 0,
                    userPinyin: '',
                    idcard: '',
					loginAccount: '',
					badgeNumber: '',
					tel: ''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					userName: [
						{ required: true, message: '请输入用户姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					userName:'',
					userType: 0,
                    userPinyin: '',
                    idcard: '',
					loginAccount: '',
					badgeNumber: '',
					tel: ''
                }
			}
		},
		methods: {
			formatCreateDate: function (row, column) {
				return row.createDate = (!row.createDate || row.createDate == '') ? '' : util.formatDate.format(new Date(row.createDate), 'yyyy-MM-dd hh:mm:ss');
			},
			formatUpdateDate: function (row, column) {
				return row.updateDate = (!row.updateDate || row.updateDate == '') ? '' : util.formatDate.format(new Date(row.updateDate), 'yyyy-MM-dd hh:mm:ss');
			},
            formatUserType: function (row, column) {
                if(row.userType == 0){
                    return "编制员工";
                }else if(row.userType == 1){
                    return "合同制员工";
                }else if(row.userType == 2){
                    return "第三方员工";
                }else{
                    return "未知";
                }
			},
			handleCurrentPageChange: function (pageNum) {// 翻页动作
                this.pageNum = pageNum;
                this.getList();// 翻页后刷新页面数据
			},
			//获取列表
			getList() {
				let params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					userName: this.filters.userName
				};
				this.listLoading = true;
				//NProgress.start();
				getUserList(params).then(function(result){
					this.tableData = result.data.list;
					this.pageNum = result.data.pageNum;
					this.pageSize = result.data.pageSize;
					this.total = result.data.total;
					this.listLoading = false;
				}.bind(this)).catch(function (error) {
					this.listLoading = false;
					console.log(error);
				}.bind(this));
			},
			//删除
			userDelete: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let params = {id:row.id};
					deleteUser(params).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getList();
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			//显示编辑界面
			userEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			usertAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
					userName:'',
					userType: 0,
                    userPinyin: '',
                    idcard: '',
					loginAccount: '',
					badgeNumber: '',
					tel: ''
                }
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
                            let params = Object.assign({}, this.editForm);
                            params.createDate = Date.parse(new Date(params.createDate));
							editUser(params).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getList();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
                            let params = Object.assign({}, this.addForm);
							addUser(params).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getList();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getList();
		}
	}

</script>
